/*
 * @Author: ChunLai
 * @Date: 2023-08-08 08:53:31
 * @LastEditTime: 2025-11-21 09:28:50
 * @Description:银行卡签约绑卡
 * @FilePath: \01.latest-short-insure\src\mixins\banksign.js
 */
import { rebackUrlPath } from "@/methods/format.js";

export const bankCardSignHandleMixin = {
  data() {
    return {
      bindId: "",
      transactionId: "", //交易串号
      authTransrNo: "", //序列号
      signConfirmType: "", //签约确认方式
      signType: "01", //签约类型，01签约，02解约
      signFlag: "-1",
    };
  },
  computed: {
    signText() {
      let res = "签约查询";
      if (this.signFlag == "1") {
        res = "签约申请";
      } else if (this.signFlag == "2") {
        res = "签约确认";
      } else if (this.signFlag == "4") {
        res = "去解约";
      } else if (this.signFlag == "5") {
        res = "解约确认";
      }
      return res;
    },
  },
  methods: {
    signBeforeQuery() {
      this.$store
        .dispatch("apis/comConfigApi", {
          apiParams: this.apiComParams,
          apiName: "paymentAuthQuery",
        })
        .then((data) => {
          this.handleCode(data.code, data.msg, data);
        });
    },
    // 申请
    payAuthApplyApi(type) {
      this.signType = type;
      let rePath = `#/form`;
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        transactionId: this.transactionId,
        authTransrNo: this.authTransrNo,
        signType: this.signType,
        successUrl: `${rebackUrlPath(`${rePath}`)}`,
        failUrl: `${rebackUrlPath(`${rePath}`)}`,
      };
      this.$store
        .dispatch("apis/comConfigApi", {
          apiParams: params,
          apiName: "paymentAuthApply",
        })
        .then((data) => {
          this.handleCode(data.code, data.msg, data);
        });
    },
    // 确认
    payAuthSureApi(type = "", smsCode = "") {
      if (type) {
        this.signType = type;
      }
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        transactionId: this.transactionId,
        authTransrNo: this.authTransrNo,
        signType: this.signType,
        smsCode: smsCode, //手机收到的短信验证码
      };
      this.$store
        .dispatch("apis/comConfigApi", {
          apiParams: params,
          apiName: "paymentAuthConfirm",
        })
        .then((data) => {
          this.handleCode(data.code, data.msg, data);
        });
    },
    handleCode(apiCode, apiMsg = "", apiRes) {
      if (apiRes.transactionId) {
        this.transactionId = apiRes.transactionId;
        this.$store.commit("insert/UPDATE_PAYINFO", {
          transactionId: this.transactionId,
        });
      }
      if (apiRes.authTransrNo) {
        this.authTransrNo = apiRes.authTransrNo;
        this.$store.commit("insert/UPDATE_PAYINFO", {
          authTransrNo: this.authTransrNo,
        });
      }
      if (apiRes.bindId) {
        this.bindId = apiRes.bindId;
        this.$store.commit("insert/UPDATE_PAYINFO", { bindId: this.bindId });
      }
      if (apiRes.signConfirmType) {
        this.signConfirmType = apiRes.signConfirmType;
      }
      if (apiRes.url) {
        this.signUrl = apiRes.url;
      }
      this.signFlag = apiCode;
      if (apiCode === "3") {
        this.$baseAlert(apiMsg, () => {
          if (apiRes.bindId) {
            this.saveBindParams();
          } else {
            this.toNextPage();
          }
        });
      } else if (["0", "6"].includes(apiCode)) {
        this.$baseAlert(`${apiMsg || "签约失败,请稍候重试"}`, () => {
          // this.$router.push("/replace");
        });
      } else {
        let sureTxt = `${
          apiCode == "1"
            ? "签约申请"
            : apiCode == "2"
            ? "签约确认"
            : apiCode == "4"
            ? "去解约" // 调解约申请
            : apiCode == "5"
            ? "解约确认" // 调解约确认
            : ""
        }`;
        this.$baseConfirm(
          `${apiMsg}`,
          () => {
            this.signCodeHandle(apiCode);
          },
          () => {
            // this.$router.push('/replace');
          },
          `${sureTxt}`
        );
      }
    },
    signCodeHandle(apiCode) {
      if (apiCode == "1") {
        this.payAuthApplyApi("01");
      } else if (apiCode == "2") {
        this.signType = "01";
        //签约确认方式，1短信验证码 ，2短信回复，3跳转链接(此类型会返回url字段)
        if (this.signConfirmType == "1") {
          this.$refs.smsCodeRef.initModule(this.cont.firBankMobile);
        } else if (this.signConfirmType == "2") {
          this.$baseAlert("请尽快完成短信回复,查询签约确认");
        } else if (this.signConfirmType == "3") {
          window.location.href = this.signUrl;
        }
      } else if (apiCode == "4") {
        this.payAuthApplyApi("02");
      } else if (apiCode == "5") {
        this.payAuthSureApi("02");
      }
    },
  },
};
